import {defineStore} from 'pinia';
import {RequestAPI} from "@/utils/RequestAPI";
import {StringUtils} from "@/utils/StringUtils";
import {StoreUtils} from "../utils/StoreUtils";

export const useDictionaryStore = defineStore('dictionaryService',
  {
    state() {
      return {}
    },
    actions: {
      async getDeviceList(payload) {
        let api = new RequestAPI()
        try {
          let {
            dateRange,
            limit,
            size,
            searchKey
          } = payload || {};

          let params = {
            limit: limit || 1,
            size: size || 10,
            // 过滤传感器设备
            flag:1,
            di_name:searchKey
          }
          let [st, et] = dateRange;
          if (st && et) {
            params['startTime'] = st;
            params['endTime'] = et;
          }

          let res = await api.get('deviceinfo', params);

          return res;
        } catch (err) {
          throw new Error(err);
        }
      },
      async getPlantProcessList(payload) {
        let api = new RequestAPI()
        try {
          let {
            dateRange,
            limit,
            size,
            searchKey
          } = payload || {};

          let params = {
            limit: limit || 1,
            size: size || 5,
            searchKey
          }
          let [st, et] = dateRange;
          if (st && et) {
            params['startTime'] = st;
            params['endTime'] = et;
          }

          let res = await api.get('planttecharchives', params);

          return res;
        } catch (err) {
          throw new Error(err);
        }
      },
      async getUnitsList(payload) {
        let api = new RequestAPI()
        try {
          let {
            limit,
            size,
            searchKey
          } = payload || {};

          let params = {
            limit: limit || 1,
            size: size || 999,
            searchKey
          }

          let res = await api.get('baseunit', params);

          return res;
        } catch (err) {
          throw new Error(err);
        }
      },
      async getSysDictionary(payload) {
        let api = new RequestAPI()
        try {
          let {
            searchKey = '',
            dict_name
          } = payload || {};


          if (StringUtils.isEmptyOrNull(dict_name)) {
            console.error('dict_name can not be empty.')
            return;
          }

          let res = await api.get(`data-items/${dict_name}/detail/tree`, {
            keyword: searchKey
          });

          return res;
        } catch (err) {
          throw new Error(err);
        }
      },
      async getUserListOld(payload) {
        let api = new RequestAPI()
        try {
          let {
            limit,
            size,
            searchKey
          } = payload || {};

          let params = {
            limit: limit || 1,
            size: size || 10,
            Keyword:searchKey,
            Company_Id:'',
            F_EnabledMark:1
          }

          let res = await api.get('users/page', params);

          return res;
        } catch (err) {
          throw new Error(err);
        }
      },
      async getUserList(payload) {
        let api = new RequestAPI()
        try {
          let {
            limit,
            size,
            searchKey,
            dept_ids
          } = payload || {};

          let params = {
            limit: limit || 1,
            size: size || 10,
            Keyword:searchKey,
            Company_Id:'',
            F_EnabledMark:1,
            Department_Id:dept_ids
          }

          let res = await api.get('xc/users/page', params);

          return res;
        } catch (err) {
          throw new Error(err);
        }
      },
      async getBiiTypeTree( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/data-sources/db4ed04aa3810200718330971382af49/tree"),
            method: "get",
            params: {
              field: "bic_id",
              parentfield: "bic_p_id"
            }
          })

          const { data: Rows = [], Total = 0, msg, code } = res

          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getBiiList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/bllclass"),
            method: "get",
            params: {
              ...payload
            }
          })

          const { data, msg, code } = res

          const { Rows = [], Total = 0 } = data || {}

          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getBctList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/customer"),
            method: "get",
            params: {
              ...payload,
              bc_state: 1
            }
          })

          const { data, msg, code } = res

          const { Rows = [], Total = 0 } = data || {}

          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getBwlList( payload = {}) {
        let api = new RequestAPI()
        const res = await api.request({
          url: StoreUtils.parseUrl("BaseWarehouseLocation"),
          method: "get",
          params: {
            ...payload
          }
        })
        const { data, msg, code } = res
        const { Rows = [], Total = 0 } = data || {}

        return {
          Rows,
          Total,
          msg,
          code
        }
      },
      /**
       * 仓库
       * @param commit
       * @param payload
       * @returns {Promise<{msg, code, Total: number, Rows: *[]}>}
       */
      async getBwaList( payload = {}) {
        let api = new RequestAPI()
        const res = await api.request({
          url: StoreUtils.parseUrl("basewarehousearchives"),
          method: "get",
          params: {
            ...payload
          }
        })
        const { data, msg, code } = res
        const { Rows = [], Total = 0 } = data || {}

        return {
          Rows,
          Total,
          msg,
          code
        }
      },
    }
  }
)
